package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.bean.selfinfo.CouponsInfo;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.Tools;
import com.youmingxing.view.CouponsView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡券活动
 */


public class EventsCardActivity extends BaseActivity implements View.OnClickListener {

    private EditText mCardEditText;
    private Button mChangeButton;
    private String cardId;
    private List<CouponsInfo> mCouponsInfoLists = new ArrayList<>();

    private LinearLayout mConponsLayout;
    private RelativeLayout mRelativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_events_card);
        initView();
    }


    private void initView() {
        setTopbarName("活动券");
        mCardEditText = (EditText) this.findViewById(R.id.et_voucher);
        mChangeButton = (Button) this.findViewById(R.id.btn_change);
        mConponsLayout = (LinearLayout) this.findViewById(R.id.ll_Coupons);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl_progress);
        mChangeButton.setOnClickListener(this);
        AllStarApi.showCoupons(initHandler);

    }

    private void initData() {
        for (final CouponsInfo mCouponsInfoList : mCouponsInfoLists) {
            CouponsView couponsView = new CouponsView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, Tools.dip2px(this, 125));
            if (mCouponsInfoList.getStatus() == 0) {
                params.bottomMargin = Tools.dip2px(this, 13);
                couponsView.setBackgroundResource(R.color.not_used_coupons_background);
                couponsView.tv_name.setText(mCouponsInfoList.getName());
                couponsView.tv_description.setText(mCouponsInfoList.getDescription());
                couponsView.btn_change.setText("立即使用");
                couponsView.btn_change.setTag(mCouponsInfoList.getCardno());
                couponsView.btn_change.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Button button_change = (Button) v;
                        String cardno = button_change.getTag().toString();
                        Intent intent = new Intent(EventsCardActivity.this, ChooseMyAuditionsActivity.class);
                        intent.putExtra("u_id", GlobalDataManager.userId);
                        intent.putExtra("cardno",mCouponsInfoList.getCardno());
                        intent.putExtra("tips",mCouponsInfoList.getTips());
                        startActivity(intent);
                    }
                });
                couponsView.tv_intro.setText(mCouponsInfoList.getIntro());
            } else if (mCouponsInfoList.getStatus() == 1) {
                params.bottomMargin = Tools.dip2px(this, 13);
                couponsView.setBackgroundResource(R.color.coupons_background);
                couponsView.tv_name.setText(mCouponsInfoList.getName());
                couponsView.tv_description.setText(mCouponsInfoList.getDescription());
                couponsView.btn_change.setText("已使用");
                couponsView.tv_intro.setText(mCouponsInfoList.getIntro());
            } else if (mCouponsInfoList.getStatus() == 2) {
                params.bottomMargin = Tools.dip2px(this, 13);
                couponsView.setBackgroundResource(R.color.coupons_background);
                couponsView.tv_name.setText(mCouponsInfoList.getName());
                couponsView.tv_description.setText(mCouponsInfoList.getDescription());
                couponsView.btn_change.setText("立即使用");
                couponsView.tv_intro.setText(mCouponsInfoList.getIntro());
            }
            couponsView.setLayoutParams(params);
            mConponsLayout.addView(couponsView);
        }
    }


    @Override
    public void onClick(View v) {
        cardId = mCardEditText.getText().toString().trim();
        if (TextUtils.isEmpty(cardId)) {
            showToast("请输入您要兑换的卡券码");
        } else {
            // 查询卡券信息 8931506  8931554_70
            AllStarApi.searchCoupons(cardId, new BaseJsonHandler(this));
        }
    }


    BaseJsonHandler initHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONArray data = response.optJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.optJSONObject(i);
                CouponsInfo couponsInfo = new CouponsInfo();
                couponsInfo.setId(jsonObject.optInt("id"));
                couponsInfo.setStatus(jsonObject.optInt("status"));
                couponsInfo.setDescription(jsonObject.optString("description"));
                couponsInfo.setName(jsonObject.optString("name"));
                couponsInfo.setActivityid(jsonObject.optInt("activityid"));
                couponsInfo.setCardno(jsonObject.optString("cardno"));
                couponsInfo.setTips(jsonObject.optString("tips"));
                couponsInfo.setIntro(jsonObject.optString("intro"));
                mCouponsInfoLists.add(couponsInfo);
            }

            mRelativeLayout.setVisibility(View.INVISIBLE);

            initData();
        }
    };
}
